﻿using System;
using System.Collections.Generic;

namespace CRM.Database.Model
{
    public partial class Addresses : ISetChangedUser, IGetKeyValue
    {
        #region Implementation of ISetChangedUser

        /// <summary>
        /// Установка пользователя, который сделал изменение
        /// </summary>
        /// <param name="userLogin">Логин пользователя</param>
        public void SetChangedUser(string userLogin)
        {
            if (string.IsNullOrEmpty(userLogin))
                throw new Exception("Логин пользователя не должен быть пустым");

            IdUser = userLogin;
        }

        #endregion

        /// <summary>
        /// Русское название для города
        /// </summary>
        public string SettlementNameRu
        {
            get
            {
                if (Settlements != null)
                    return Settlements.NameRu;
                return "";
            }
        }

        public string CityLocalised
        {
            get 
            {
                if (IdSettlement > 0)
                {
                    var city = DB.Entities.Settlements.Find(IdSettlement);
                    if(city!=null)
                    return city.NameLocalized;
                }
                return "";
            }
        }

        public List<Regions> Regions
        {
            get { return new List<Regions>(profineRegion.Regions); }
        }


        public int? IdSettlement
        {
            get { return IdAddressesItem; }
            set { IdAddressesItem = value; }
        }

        /// <summary>
        /// Клиент
        /// </summary>
        public Customers Customer
        {
            get
            {
                if (LegalEntities != null)
                    return LegalEntities.Customers;
                if (IdLegalEntity != null)
                {
                    var col = new LegalEntitiesCollection();
                    return ((LegalEntities) col.GetItemById((int) IdLegalEntity)).Customers;
                }
                return null;
            }
        }

        /// <summary>
        /// Идентификатор региона профайн
        /// </summary>
        public int IdProfineRegion
        {
            get
            {
                var col = new SettlementsCollection();

                if (IdSettlement != null) return col.GetprofineRegionIdBySettlementId((int) IdSettlement);
                return 0;
            }
        }

        /// <summary>
        /// Идентификатор district
        /// </summary>
        public int? IdDistrict
        {
            get
            {
                var col = new SettlementsCollection();

                if (IdSettlement != null) return col.GetDistrictIdBySettlementId((int) IdSettlement);
                return 0;
            }
        }

        /// <summary>
        /// Id region
        /// </summary>
        public int? IdRegion
        {
            get
            {
                var col = new SettlementsCollection();

                if (IdSettlement != null) return col.GetRegionIdBySettlementId((int) IdSettlement);
                return 0;
            }
        }


        /// <summary>
        /// Идентификатор страны
        /// </summary>
        public int? IdCountry
        {
            get
            {
                var col = new SettlementsCollection();

                if (IdSettlement != null) return col.GetCountryIdBySetllementId((int) IdSettlement);
                return 0;
            }
        }

        //protected int? idCountry;

        ///// <summary>
        ///// Текущая страна
        ///// </summary>
        //private int _currentCountry;

        ///// <summary>
        ///// Текущий регион
        ///// </summary>
        //private int _currentRegion;


        ///// <summary>
        ///// Текущий дистрикт
        ///// </summary>
        //private int _currentDistrict;

        /// <summary>
        /// Название области
        /// </summary>
        public string DistrictName
        {
            get
            {
                var col = new SettlementsCollection();
                if (IdSettlement != null)
                {
                    Districts district = col.GetDistrictBySettlementId((int) IdSettlement);

                    if (district != null) return district.GetLocalizedName();
                }

                return "";
            }
        }


        /// <summary>
        /// Локализованное название населённого пункта
        /// </summary>
        public string SettlementsLocalized
        {
            get
            {
               // if (Settlements != null) return Settlements.GetLocalizedName();
                if(IdSettlement>0)
                {
                    var sett = DB.Entities.Settlements.Find(IdSettlement);
                    if(sett!=null)
                    {
                        return sett.GetLocalizedName();
                    }
                }
                return "";
            }
        }


        /// <summary>
        /// Название области по-русски
        /// </summary>
        public string AreasNameRu
        {
            get
            {
                if (Settlements != null && Settlements.Areas != null)
                {
                    return Settlements.Areas.NameRu;
                }
                return "";
            }
        }

        /// <summary>
        /// Локализованное название области
        /// </summary>
        public string AreasNameLocalized
        {
            get
            {
                if (Settlements != null && Settlements.Areas != null)
                {
                    return Settlements.Areas.GetLocalizedName();
                }
                return "";
            }
        }

        /// <summary>
        /// Название региона по-русски
        /// </summary>
        public string RegionNameRu
        {
            get { return Region.NameRu; }
        }


        /// <summary>
        /// Локализованное название региона
        /// </summary>
        public string RegionNameLocalized
        {
            get
            {
                if (Region != null) return Region.RegionNameLocalized;
                return "";
            }
        }

        /// <summary>
        /// Регион
        /// </summary>
        public Regions Region
        {
            get
            {
                var col = new SettlementsCollection();
                if (IdSettlement != null) return col.GetRegionBySettlementId((int) IdSettlement);
                return null;
            }
        }


        /// <summary>
        /// District
        /// </summary>
        public Districts District
        {
            get
            {
                var col = new SettlementsCollection();
                if (IdSettlement != null) return col.GetDistrictBySettlementId((int) IdSettlement);
                return null;
            }
        }

        /// <summary>
        /// Название региона по-русски
        /// </summary>
        public string DistrictNameRu
        {
            get { return District.NameRu; }
        }


        /// <summary>
        /// Локализованное название district'a
        /// </summary>
        public string DistrictNameLocalized
        {
            get
            {
                if (District != null) return District.GetLocalizedName();
                return "";
            }
        }


        /// <summary>
        /// Название региона профайн по-русски
        /// </summary>
        public string ProfineRegionNameRu
        {
            get { return profineRegion.NameRu; }
        }


        /// <summary>
        /// Регион профайн
        /// </summary>
        public profineRegions profineRegion
        {
            get
            {
                var col = new SettlementsCollection();
                if (IdSettlement != null) return col.GetProfineRegionBySettlementId((int) IdSettlement);
                return null;
            }
        }


        /// <summary>
        /// Локализованное название региона профайн 
        /// </summary>
        public string ProfineRegionNameLocalized
        {
            get
            {
                if (profineRegion != null) return profineRegion.GetLocalizedName();
                return "";
            }
        }


        /// <summary>
        /// Название страны по-русски
        /// </summary>
        public string CountryNameRu
        {
            get { return Country.CountryRu; }
        }


        /// <summary>
        /// Страна
        /// </summary>
        public Countries Country
        {
            get
            {
                var col = new SettlementsCollection();
                if (IdSettlement != null) return col.GetCountryBySettlementId((int) IdSettlement);
                return null;
            }
        }


        /// <summary>
        /// Локализованное название страны
        /// </summary>
        public string CountryNameLocalized
        {
            get
            {
                if (Country != null) return Country.CountryNameLocalized;
                return "";
            }
        }

        #region Implementation of IGetKeyValue

        /// <summary>
        /// Получение ключего значения элемента
        /// </summary>
        /// <returns>Ключевое значение</returns>
        public int GetKeyValue()
        {
            return IdAddress;
        }

        #endregion
    }
}